/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.ruralLibrary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.ruralLibrary.api.entity.ContentManagementEntity;
import com.pig4cloud.pigx.ruralLibrary.api.entity.ResourceEntity;
import com.pig4cloud.pigx.ruralLibrary.mapper.ContentManagementMapper;
import com.pig4cloud.pigx.ruralLibrary.service.ContentManagementService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static cn.hutool.core.collection.CollUtil.splitList;

/**
 * 内容管理
 *
 * @author pig
 * @date 2023-10-18 16:47:17
 */
@Service
public class ContentManagementServiceImpl extends ServiceImpl<ContentManagementMapper, ContentManagementEntity> implements ContentManagementService {

    @Override
    public R getList(String contentManagementClassification) {
        if (StrUtil.isBlank(contentManagementClassification)) {
            return R.failed("请添加管理分类");
        }
        LambdaQueryWrapper<ContentManagementEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ContentManagementEntity::getContentManagementClassification, contentManagementClassification);
        if (contentManagementClassification.equals("3")) {
            wrapper.eq(ContentManagementEntity::getType,"0");
            wrapper.orderByAsc(ContentManagementEntity::getSort);
            long totalCount = this.count(wrapper);

            int pageSize = 2;

            long totalPages = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;

            List<List<ContentManagementEntity>> lists = new ArrayList<>();
            for (int i = 1; i <= totalPages; i++) {
                // 分页查询
                List<ContentManagementEntity> pageList = this.page(new Page<>(i, pageSize), wrapper).getRecords();
                // 将当前页的数据放到子列表中
                List<List<ContentManagementEntity>> subLists = splitList(pageList, pageSize);
                // 将子列表加入到结果列表中
                lists.addAll(subLists);
            }
            return R.ok(lists);
        }else if (contentManagementClassification.equals("2")){
            wrapper.orderByDesc(ContentManagementEntity::getCreateTime);
            wrapper.last("LIMIT 6");
            return R.ok(this.list(wrapper));
        }else {
            wrapper.orderByDesc(ContentManagementEntity::getCreateTime);
            wrapper.last("LIMIT 8");
            return R.ok(this.list(wrapper));
        }

    }

    @Override
    public R getSpecialSubject() {
        LambdaQueryWrapper<ContentManagementEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ContentManagementEntity::getContentManagementClassification, "3");
        wrapper.eq(ContentManagementEntity::getType, "0");
        wrapper.orderByDesc(ContentManagementEntity::getCreateTime);
        return R.ok(this.list(wrapper));
    }

    @Override
    public R pageView(Long id) {
        ContentManagementEntity byId = this.getById(id);
        Long pageView = byId.getPageView();
        byId.setPageView(pageView + 1);
        return R.ok(this.updateById(byId));
    }

    private <T> List<List<T>> splitList(List<T> list, int pageSize) {
        List<List<T>> result = new ArrayList<>();
        int count = list.size() % pageSize == 0 ? list.size() / pageSize : list.size() / pageSize + 1;
        for (int i = 0; i < count; i++) {
            int start = i * pageSize;
            int end = Math.min((i + 1) * pageSize, list.size());
            result.add(list.subList(start, end));
        }
        return result;
    }
}
